package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    public Result add(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto.toString());
        dishService.add(dto);

        return Result.success();
    }

    @GetMapping("/page")
    public Result page(DishPageQueryDTO dto) {
        log.info("菜品分页查询:{}", dto.toString());
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品：{}",ids);
        dishService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDishById(@PathVariable Long id){
        log.info("根据菜品id查询，数据回显：{}",id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    public Result getDishsByCategoryId(String categoryId){
        log.info("查询菜品集合，套餐分类id：{}",categoryId);
        Integer cId = Integer.valueOf(categoryId);
        List<Dish> list = dishService.getDishsByCategoryId(cId);
        return Result.success(list);
    }

    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        log.info("修改菜品：{}",dto.toString());
        dishService.update(dto);
        return Result.success();
    }
}
